<% content_for :head do %>
  <%= javascript_include_tag :ui %>
  <%= javascript_include_tag :sortable %>

  <script type="text/javascript" charset="utf-8">
 $(document).ready(function(){
    function send_nested_sets_new_structure(item_id, parent_id, prev_id, next_id){
      jQuery.ajax({
        type: 'POST',
        url: '<%= restructure_admin_category_groups_path %>',
        data: {node_id: item_id, parent_id: parent_id, prev_id: prev_id, next_id: next_id, authenticity_token: '<%= form_authenticity_token %>'},
        dataType: 'script',
        beforeSend: function(xhr){},
        error: function(xhr, status, error){ alert(error); }
      });
    }

    $('ol#categories').nestedSortable({
      forcePlaceholderSize: true,
      items: 'li.category',
      maxLevels: 4,
      opacity: .6,
      revert: 250,
      tabSize: 25,
      tolerance: 'pointer',

      update: function(event, ui){
        item_id   = ui.item.children("div").children(".node_id").attr("value");
        parent_id = ui.item.parent().parent("li.category").children("div").children(".node_id").attr("value");
        prev_id   = ui.item.prev("li.category").children("div").children(".node_id").attr("value");
        next_id   = ui.item.next("li.category").children("div").children(".node_id").attr("value");

        send_nested_sets_new_structure(item_id, parent_id, prev_id, next_id);
      }
    });
  });
  </script>

  <style type="text/css">
    ol li {
      cursor: move;
      line-height: 25px;
    }
  </style>
<% end %>

<%= form_for([:admin, @category_group], :validate => true) do |f| %>

<%= render "shared/error_messages", :target => @category_group %>

<table>
  <tbody>
    <tr>
      <th><%= f.label :presentation %></th>
      <td><%= f.text_field :presentation, :maxlength => 30, :size => 40 %></td>
    </tr>
    <tr>
      <th><%= f.label :root_url %></th>
      <td><%= f.text_field :root_url, :maxlength => 30, :size => 25 %> "/adresa/", nebo "/"</td>
    </tr>
    <tr>
      <td colspan="2"><%= f.submit %></td>
    </tr>
  </tbody>
</table>
<% end %>

<h2>Hierarchie kategorií</h2>

<ol id="categories">
  <% @category_group.categories.roots.each do |root| %>
    <%= render "categories", :root => root %>
  <% end %>
</ol>

<%= button_to_new "Přidat kategorii", new_admin_category_group_category_path(params[:id]) %>